<?php

namespace app\admin\controller\zhusu;

use app\admin\controller\AdminBase;

use app\model\Zhusu as R;
use app\model\Product;
use app\model\Machine_number;
use app\model\Material;
use app\model\Colour;
use app\model\system\Menus;
use app\model\system\Manager;

/**
 * 注塑报表主控制器
 */
class Reportforms extends AdminBase
{
    public function index($do='')
    {
        //dump(session(VT_MANAGER));
         $groupid=  session(VT_MANAGER)['groupid'];
        if($do=='json'){
      
            return $this->returnMsg((new R())->listQuery($groupid));
        }elseif($do=='mjson'){
            $ids  = '';
            $data = [];
            $id = $this->request->get('id/d');
            if($id){
                $rs = R::get(['id'=>$id]);
                $ids = empty($rs) ? '' : $rs['role_menuid'];
            }
            $rs = Menus::cache(); // 获取后台菜单缓存 构建zTree Json数据
            foreach($rs as $v){
                $flag = (strpos(",$ids,",",$v[menuid],")!==false) ? true : false;
                $data[] = ['id'=>$v['menuid'],'pId'=>$v['parent_id'],'name'=>$v['role_name'].' '.$v['role_url'],'checked'=>$flag,'open'=>true];
            }
            return $this->returnMsg($data);
        }
        $product = Product::order(['listorder'=>'asc'])->column('*');
        $Machine_number = Machine_number::order(['id'=>'asc'])->column('*');
        $Material = Material::order(['id'=>'asc'])->column('*');
        $Colour = Colour::order(['id'=>'asc'])->column('*');
        if( $groupid==1){
            $Manager= Manager::order(['userid'=>'asc'])->column('userid,truename');
        }else{
            $Manager= Manager::order(['userid'=>'asc'])->where(['groupid'=> $groupid])->column('userid,truename');
        }

        $this->assign([
            'limit'=> 10 ,
            'product' => json_encode($product),
            'machine_number' => json_encode($Machine_number),
            'material' => json_encode($Material),
            'colour' => json_encode($Colour),
            'operator' => json_encode($Manager),

        ]);

        $this->assign('limit', 10);
        return $this->fetch();
    }

    public function add()
    { 
       
        //$d = $this->only(['@token'=>'','role_name/*/{2,30}/角色名称','listorder/d','state/d','role_menuid']);
        $d = $this->only(['@token'=>'','order_id','product_id','number','material_id','colour_id','order_number','qualified_quantity','working_hours','scrapped_part','weight','operator','debugging_time','change_material_time','abnormal_time','remarks','classes']);
        $d['scdate'] = VT_TIME;

        $d['groupid'] = session(VT_MANAGER)['groupid'];
      
        $where['order_id']=array('like',$d['order_id']);
        $rs = R::where($where)->where(['isdel'=>0,'product_id'=>$d['product_id']])->select();
        $sum = 0;
        foreach ($rs as $key => $value) {
           $sum += $rs[$key]['qualified_quantity'];
        }
        $d['accumulate_number'] = $sum + $d['qualified_quantity'];
       
      if($rsid = R::insertGetId($d)){
           R::cache(['roleid'=>$rsid],1);
            return $this->returnMsg("添加成功", 1);
        }else{
            return $this->returnMsg('添加失败');
        }
       
    }


    public function edit($do='')
    {
        $d = $this->only(['@token'=>'','av','af','id','order_id','product_id','number','material_id','colour_id','order_number','qualified_quantity','working_hours','scrapped_part','weight','operator','debugging_time','change_material_time','abnormal_time','remarks']);
        $id = $d['id'];
        $Myobj = R::get("id = $id");
        if(!$Myobj) return $this->returnMsg("数据不存在");
       
        if($do=='up'){
            $value = $d['av'];
            $field = $d['af'];
           
            if($Myobj->save([$field=>$value])){
                R::cache($id,1);
                return $this->returnMsg("设置成功", 1);
            }else{
                return $this->returnMsg("设置失败");
            }
        }
        else{
           // $d['role_menuid'] = is_array($d['role_menuid']) ? implode(',', array_map('intval', $d['role_menuid'])) : '';
            if($Myobj->save($d)){
              //  R::cache(['roleid'=>$roleid,'role_name'=>$d['role_name'],'role_menuid'=>$d['role_menuid']],1);
                return $this->returnMsg("编辑成功", 1);
            }else{
                return $this->returnMsg("编辑失败");
            }
        }
        
    }

    public function del()
    {
        $id = $this->only(['@token'=>'','id'])['id'];
        $id = is_array($id) ? implode(',',$id) : $id;
        if(!$id) return $this->returnMsg('参数错误');
      //R::del("id IN($id)") 
        if(R::isdel("id IN($id)")){
            $ids = explode(',',$id);
            foreach($ids as $id){
                cache('VMENUS_1_'.$id, null);
            }
            return $this->returnMsg("删除成功", 1);
        }else{
            return $this->returnMsg("删除失败,注意审批通过的将不能删除！");
        }
        
    }

    public function look($do='')
    {
        $roleid  = session(VT_MANAGER)['roleid'];
        $groupid = session(VT_MANAGER)['groupid'];
     // echo  $roleid;
        if($do=='json'){
            return $this->returnMsg((new R())->today_listQuery($roleid,$groupid));
        }elseif($do=='shenpi'){
            
            $jh = $this->only(['@token'=>'','shenpiid','shenpicontent','checkRows']);
            if($jh['shenpiid']==0){return $this->returnMsg("审核不通过");}else{
                if($roleid == 1){$gid =4;}
                else if($roleid == 4){$gid =6;}
                else if($roleid == 6){$gid =3;}

                foreach($jh['checkRows'] as $k=>$v){

                   $rup= R::update(['isexamine' =>$gid,'shenpicontent'=>$jh['shenpicontent']], ['id' => $jh['checkRows'][$k]['id']]);
                    
                }

                if($rup){
                   
                    return $this->returnMsg("审核成功", 1);
                }else{
                    return $this->returnMsg("修改失败！");
                }
            }
         
        }

        $t = date("Y-m-d");
        $this->assign('date',$t);
        $this->assign('limit', 14);
        $this->assign('roleid',$roleid);
        return $this->fetch();
       
    }

    public function product($do='')
    {
        $product = Product::order(['listorder'=>'asc']);
        if($do=='json'){
            return $this->returnMsg(Product::order(['listorder'=>'asc'])->select());
        }
        
        return $this->fetch();
    }

    public function product_add()
    { 
        $d = $this->only(['@token'=>'','parentid','name','unitprice','modulus','listorder']);

      if($rsid = Product::insertGetId($d)){
        Product::cache(['product_id'=>$rsid],1);
            return $this->returnMsg("添加成功", 1);
        }else{
            return $this->returnMsg('添加失败');
        }
       
    }

    public function product_edit($do='')
    {
        $d = $this->only(['@token'=>'','av','af','id','parentid','name','unitprice','modulus','listorder']);
        $id = $d['id'];
        $Myobj = Product::get("id = $id");
        if(!$Myobj) return $this->returnMsg("数据不存在");
       
        if($do=='up'){
            $value = $d['av'];
            $field = $d['af'];
        
            if($Myobj->save([$field=>$value])){
                Product::cache($id,1);
                return $this->returnMsg("设置成功", 1);
            }else{
                return $this->returnMsg("设置失败");
            }
            
        }
        else{
            
            if($Myobj->save($d)){
              //  R::cache(['roleid'=>$roleid,'role_name'=>$d['role_name'],'role_menuid'=>$d['role_menuid']],1);
                return $this->returnMsg("编辑成功", 1);
            }else{
                return $this->returnMsg("编辑失败");
            }
        }
        
    }

    public function product_del()
    {
        $id = $this->only(['@token'=>'','id'])['id'];
        $id = is_array($id) ? implode(',',$id) : $id;
        if(!$id) return $this->returnMsg('参数错误');
    
        if(Product::del("id IN($id)")){
            $ids = explode(',',$id);
            foreach($ids as $id){
                cache('VMENUS_1_'.$id, null);
            }
            return $this->returnMsg("删除成功", 1);
        }else{
            return $this->returnMsg("删除失败！");
        }
        
    }

    public function  product_adds()
    {
        $d = $this->only(['@token'=>'','titles/h','pid/d','id/d']);
        if(!$d['titles']) return $this->returnMsg("请输入产品名称");
        $id = $d['pid'];
        $rs = Product::get(['id'=>$id]);
        
        if($id==0 || $rs){
            $data = [];
            $arr  = explode("\n", $d['titles']);
            foreach($arr as $v){
                if(!is_preg($v,'{2,20}')) continue;
                $data[] = ['name'=>$v,'parentid'=>$id,'listorder'=>10,];
            }
            if(Product::insertAll($data)){
                Product::cache(1);
                return $this->returnMsg("批量添加成功", 1);
            }else{
                return $this->returnMsg('批量添加失败');
            }
        }else{
            return $this->returnMsg("上级ID不存在");
        }
        
    }
    public function  dailyreport($do='')
    {
        $roleid  = session(VT_MANAGER)['roleid'];
        $groupid = session(VT_MANAGER)['groupid'];
     // echo  $roleid;
        if($do=='json'){
            return $this->returnMsg((new R())->statistics_today_listQuery($roleid,$groupid));
        }

        $t = date("Y-m-d");
        $this->assign('date',$t);
        $this->assign('limit', 14);
        $this->assign('roleid',$roleid);
        return $this->fetch();
        
    }

   







   

}
    